package ca.uwindsor.cs.deepweb.estimation.method.border;

import java.util.HashSet;
import java.util.Set;

public class MatrixLimited extends MatrixDEV {

	protected HashSet<String> docblacklist;

	public MatrixLimited(String FieldName, String[] wordlistA) {
		super(FieldName, wordlistA);
	}

	/**
	 * @param i
	 * @param documents
	 */
	public void addQuery(int i, Set<String> documents) {

		int[] array = null;
		if (documents.size() != 0) {
			array = new int[documents.size()];
			index = 0;
			// add new documents to unique id list
			for (String s : documents) {
				if (!docid_index.containsKey(s)) {
					p = docid_index.size();
					docid_index.put(s, p);
				} else {
					p = docid_index.get(s).intValue();
				}
				array[index] = p;
				index++;
			}
		}
		// add to term-docs list
		term_docslist[i] = array;
	}

	/**
	 * 
	 */
	public void calculateQueryWeight() {
		int[] l;
		float weight;
		for (index = 0; index < terms.length; index++) {
			l = term_docslist[index];
			weight = 0;
			if (l != null) {
				for (int i : l) {
					if (inverseDocumentWeight[i] <= 1)
						weight += 1 / (float) inverseDocumentWeight[i];
				}
			}
			QueryWeight[index] = weight;
		}
	}

	/**
	 * @param DA 
	 * @param DB 
	 * 
	 */
	public void calculateQueryWeight(Set<String> DA, Set<String> DB) {
		DA.removeAll(DB);
		HashSet<Integer> blacklist = new HashSet<Integer>(DA.size());
		for (String id : DA) {
			blacklist.add(docid_index.get(id));
		}
		int[] l;
		float weight;
		for (index = 0; index < terms.length; index++) {
			l = term_docslist[index];
			weight = 0;
			if (l != null) {
				for (int i : l) {
					if (!blacklist.contains(new Integer(i)))
						weight += 1 / (float) inverseDocumentWeight[i];
				}
			}
			QueryWeight[index] = weight;
		}
	}

	/**
	 * @param i
	 * @param documentsA
	 * @param documentsB
	 */
	public void addQuery(int i, Set<String> documentsA, Set<String> documentsB) {
		// documentsA.retainAll(documentsB);// DA intersection DB
		int[] array = null;
		if (documentsA.size() != 0) {
			array = new int[documentsA.size()];
			index = 0;
			// add new documents to unique id list
			for (String s : documentsA) {
				if (!docid_index.containsKey(s)) {
					p = docid_index.size();
					docid_index.put(s, p);
				} else {
					p = docid_index.get(s).intValue();
				}
				array[index] = p;
				index++;
			}
		}
		// add to term-docs list
		term_docslist[i] = array;
	}

}